﻿//****************************************************************************
// Author Scile
// Site www.scile.cn
// Version 1.0
// Fri Feb 02 15:55:15 2009
//****************************************************************************
// Language Version: 	ActionScript 3.0
// Runtime Versions: 	Flash Player 9
//****************************************************************************

package com.jacky{
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.*;
	import flash.events.TimerEvent;
	import flash.geom.ColorTransform;
	import flash.utils.Timer;

	public dynamic class ImageLoader extends Loader {

		public var fadeType:String;
		public var centerFlag:Boolean = false;
		public var centerFlag2:Boolean = false;
		private var initWidth:Object;
		private var initHeight:Object;
		private var initSmoothing:Boolean;
		private var timer:Timer;
		
		public static const WHITE:String = "white";
		public static const BLACK:String = "black";
		public static const ALPHA:String = "alpha";

		public function ImageLoader(url:String = null, width:Number = 0, height:Number = 0, smoothing:Boolean = false) {
			fadeType = ImageLoader.WHITE;
			
			timer = new Timer(10,51);
			timer.addEventListener(TimerEvent.TIMER, timerHandler);
			contentLoaderInfo.addEventListener(Event.INIT, initHandler);
			
			if (url != null) loadImage(url, width, height, smoothing);
			this.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
		}
		
		public function loadImage(url:String, width:Number = 0, height:Number = 0, smoothing:Boolean = false):void{
			initWidth = width;
			initHeight = height;
			initSmoothing = smoothing;
			timer.reset();
			load(new URLRequest(url));
		}
		private function initHandler(e:Event):void {
			e.target.content.smoothing = initSmoothing;
			e.target.content.width = (initWidth == 0) ? contentLoaderInfo.width : initWidth;
			e.target.content.height = (initHeight == 0) ? contentLoaderInfo.height : initHeight;
			alpha = 0;
			if(centerFlag){
				e.target.content.x = (240-e.target.content.width)/ 2;
			}else if (centerFlag2) {
				e.target.content.x = (965-e.target.content.width)/ 2;	
			}
			//trace(e.target.content.width,e.target.content.height)
			timer.start();
		}
		
		private function timerHandler(e:TimerEvent):void {
			var ct:ColorTransform = new ColorTransform();
			var pct:Number = timer.currentCount / timer.repeatCount;
			
			switch(fadeType){
				case ImageLoader.WHITE:
					ct.redOffset   = 255 - 255 * pct;
					ct.greenOffset = 255 - 255 * pct;
					ct.blueOffset  = 255 - 255 * pct;
					transform.colorTransform = ct;
					break;
				case ImageLoader.BLACK:
					ct.redOffset   = 255 * pct - 255;
					ct.greenOffset = 255 * pct - 255;
					ct.blueOffset  = 255 * pct - 255;
					transform.colorTransform = ct;
					break;
				case ImageLoader.ALPHA:
					ct.alphaMultiplier = pct;
					transform.colorTransform = ct;
					break;
				default:
					transform.colorTransform = ct;
			}	
		}
		
		private function removedFromStageHandler(e:Event):void {
			this.removeEventListener(Event.REMOVED_FROM_STAGE, removedFromStageHandler);
		}

	}
}